package com.woyou.ui.fragment;

import java.util.List;

import retrofit.RetrofitError;
import android.annotation.SuppressLint;
import android.content.Context;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.text.Html;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AbsListView;
import android.widget.AbsListView.OnScrollListener;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.citaq.ideliver.R;
import com.handmark.pulltorefresh.library.PullToRefreshAdapterViewBase.OnPreRefreshingAnimListener;
import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshBase.OnLastItemVisibleListener;
import com.handmark.pulltorefresh.library.PullToRefreshBase.OnRefreshListener;
import com.handmark.pulltorefresh.library.PullToRefreshListView;
import com.squareup.picasso.Picasso;
import com.woyou.Constant;
import com.woyou.bean.ChosenGoods;
import com.woyou.bean.CodeListResult;
import com.woyou.bean.Gift;
import com.woyou.bean.Goods;
import com.woyou.bean.Info;
import com.woyou.bean.Result;
import com.woyou.bean.ShopDetail;
import com.woyou.bean.ShopItem;
import com.woyou.bean.Support;
import com.woyou.bean.rpc.ActivitiesReq;
import com.woyou.bean.rpc.GoodsListReq;
import com.woyou.service.NetWorkCenter;
import com.woyou.ui.activity.ConfirmOrderActivity_;
import com.woyou.ui.activity.ShopInfoActivity;
import com.woyou.ui.activity.ShopInfoActivity.FragmentControl;
import com.woyou.ui.adapter.ShopMenuListAdapter;
import com.woyou.ui.component.ErrorHintView;
import com.woyou.ui.component.ErrorHintView.OperateListener;
import com.woyou.ui.component.SuperUI;
import com.woyou.ui.view.SuperLayout;
import com.woyou.utils.LogUtil;
import com.woyou.utils.ParseUtils;
import com.woyou.utils.Task;
import com.woyou.utils.Utils;
import com.woyou.utils.eventbus.EventGiftListChange;
import com.woyou.utils.eventbus.EventRefPrice;
import com.woyou.utils.eventbus.EventRestShoppingCarData;
import com.woyou.utils.eventbus.EventShoppingCarChange;
import com.woyou.utils.eventbus.EventShowDetails;
import com.woyou.utils.eventbus.SendOrderData;

import de.greenrobot.event.EventBus;

/**
 * 商品列表
 * 
 * @author lenovo
 * 
 */
public class ShopInfoMenuLayout extends SuperLayout implements OnClickListener, OnScrollListener {

	private static final String TAG = "ShopInfoMenuLayout";

	// 菜品列表
	private PullToRefreshListView item_menulist_lv;
	public ShopMenuListAdapter listAdapter;
	private View headView;
	private Drawable tu;

	private List<Object> adapterData = mGoodsModel.getDataSource();

	// 购物车
	public ShoppingCarLayout layout_menulist_shoppingcar;
	private TextView item_menulist_summoney;
	private RelativeLayout item_menulist_shoppingcar;
	private TextView item_menulist_shoppingcar_tv;
	private TextView item_menulist_submit;
	private FrameLayout item_menulist_bussiness;
	private ShopItem item = null;
	Result<List<Goods>> goodsListRes;
	GoodsListReq goodsListReq = new GoodsListReq();
	public ShopDetail shopDetail; // 店铺详情
	private PropertyLayout propertylayout; // 商品属性

	// 加载错误提示
	RelativeLayout item_menulist;
	ErrorHintView hintView;
	private LinearLayout footerView; // 最后一条listview

	/**
	 * 显示店铺列表
	 */
	private int VIEW_SHOPLIST = 1;
	/**
	 * 显示断网
	 */
	private int VIEW_WIFIFAILUER = 2;
	/**
	 * 显示加载数据失败
	 */
	private int VIEW_LOADFAILURE = 3;
	/**
	 * 显示加载中
	 */
	private int VIEW_LOADING = 4;
	/**
	 * 显示请求超时
	 */
	private int VIEW_OUTTIME = 5;

	private LayoutControl mLayoutControl = new LayoutControl() {

		@Override
		public void showPropView(Goods goods, ChosenGoods adjustChosen) {
			propertylayout.showPropView(goods, adjustChosen);
			propertylayout.setVisibility(View.VISIBLE);
		}

		@Override
		public void showPropView(Goods goods) {
			propertylayout.showPropView(goods, null);
			propertylayout.setVisibility(View.VISIBLE);
		}

		@Override
		public void checkGiftList() {
			ShopInfoMenuLayout.this.checkGiftList();
			
		}
	};

	public ShopInfoMenuLayout(Context context) {
		super(context);
		findView();
		EventBus.getDefault().register(this);
	}

	/**
	 * 属性面板是否显示
	 * 
	 * @return
	 */
	public boolean isPropPanelShow() {
		int visibility = propertylayout.getVisibility();
		if (View.VISIBLE == visibility) {
			return true;
		}
		return false;
	}

	/**
	 * 隐藏属性面板
	 */
	public void hidePropPanel() {
		propertylayout.hide();
	}

	private void findView() {
		View.inflate(getContext(), R.layout.layout_menulist, this);
		Resources res = getResources();
		Bitmap bitmap = BitmapFactory.decodeResource(res, R.raw.icon_nosupport);
		tu = new BitmapDrawable(res, bitmap);
		item_menulist = (RelativeLayout) findViewById(R.id.item_menulist);
		hintView = (ErrorHintView) findViewById(R.id.hintView);
		//
		item_menulist_lv = (PullToRefreshListView) findViewById(R.id.item_menulist_lv);
		layout_menulist_shoppingcar = (ShoppingCarLayout) findViewById(R.id.layout_menulist_shoppingcar);
		layout_menulist_shoppingcar.setLayoutControl(mLayoutControl);
		item_menulist_summoney = (TextView) findViewById(R.id.item_menulist_summoney);
		item_menulist_shoppingcar_tv = (TextView) findViewById(R.id.item_menulist_shoppingcar_tv);
		item_menulist_shoppingcar = (RelativeLayout) findViewById(R.id.item_menulist_shoppingcar);
		item_menulist_submit = (TextView) findViewById(R.id.item_menulist_submit);
		item_menulist_submit.setOnClickListener(this);
		item_menulist_bussiness = (FrameLayout) findViewById(R.id.business_frag);

		if (listAdapter == null) {
			listAdapter = new ShopMenuListAdapter(getContext(), adapterData);
			item_menulist_lv.setAdapter(listAdapter);
			listAdapter.setLayoutControl(mLayoutControl);
			setupIdleListener(item_menulist_lv.getRefreshableView());
		}

		initPropertyLayout();

		// 判断是否有网络
		netFailOperate();
	}

	/**
	 * 购物车发生改变
	 * 
	 * @param event
	 */
	public void onEvent(EventShoppingCarChange event) {
		Log.i(TAG, "EventShoppingCarChange");
		mGoodsModel.dataMatching();
		listAdapter.notifyDataSetChanged();
	}

	/**
	 * 刷新总价
	 * 
	 * @param event
	 */
	public void onEvent(EventRefPrice event) {
		Log.i(TAG, "EventRefUIPrice");
		refreshSumMoney();
	}

	public void onEvent(EventGiftListChange event) {
		refreshSumMoney();
	}

	/**
	 * 显示套餐明细
	 * 
	 * @param event
	 */
	public void onEvent(EventShowDetails event) {
		Log.i(TAG, "EventShowDetails");
		propertylayout.showDetailsView(event.getData());
		propertylayout.setVisibility(View.VISIBLE);
	}

	/**
	 * 清空数据
	 */
	public void clearData() {
		if (adapterData != null) {
			if (footerView != null) { // 删除最底部的一条
				item_menulist_lv.getRefreshableView().removeFooterView(footerView);
				footerView = null;
			}
			adapterData.clear();
			listAdapter.notifyDataSetChanged();
		}
	}

	/**
	 * 初始化View
	 */
	public void initView(ShopItem item, ShopDetail shopDetail, Result<List<Goods>> goodsList) {
		pageNext = goodsList.getPage();
		this.item = item;
		this.shopDetail = shopDetail;
		goodsListRes = goodsList;
		// 初始化购物车
		initShoppingCar();
		// 初始化菜品列表
		initMenuListData();
		// 初始化属性页面
		// initPropertyLayout();
	}

	/**
	 * 初始化购物车
	 */
	public void initShoppingCar() {
		layout_menulist_shoppingcar.setShopDetail(shopDetail);
		layout_menulist_shoppingcar.setVisibility(View.GONE);
	}

	/**
	 * 初始化属性页面
	 */
	private void initPropertyLayout() {
		propertylayout = (PropertyLayout) findViewById(R.id.layout_menulist_propertylayout);
		propertylayout.setLayoutControl(mLayoutControl);
		propertylayout.setVisibility(View.GONE);
	}

	/**
	 * 初始化菜品列表
	 */
	private void initMenuListData() {

		// 初始化购物车样式
		if (mShoppingCarModel.getChosenGoodsList().size() <= 0) {
			if (item != null) {
				if (shopDetail.getSendFee() < 0) {
					shopOutsideRange();
				} else {
					if (item.getState() == 2) { // 休息
						shopNotBussiness();
					} else if (item.getState() == 3) { // 繁忙
						onlyAcceptReservation();
					} else {  // 营业 item.getState()==1
						notSelected();
					}
				}
			} else {
				notSelected();
			}
		}
		
		listAdapter.setShopItem(item);

		// goodsListReq = new GoodsListReq();
		goodsListReq.setsId(item.getsId());
		pageNext = goodsListRes.page;
		constructionData();// 构建ListView的数据源
		// 重置header
		if (headView != null) {
			item_menulist_lv.getRefreshableView().removeHeaderView(headView);
		}
		// 添加店铺支持属性
		if (shopDetail.getSupportList() != null && shopDetail.getSupportList().size() > 0) {
			headView = View.inflate(getContext(), R.layout.menulist_lv_header, null);
			item_menulist_lv.getRefreshableView().addHeaderView(headView);
			LinearLayout header_layout = (LinearLayout) headView.findViewById(R.id.menulist_lv_header_layout);
			List<Support> supports = shopDetail.getSupportList();
			for (int i = 0; i < supports.size(); i++) {
				View itemView = View.inflate(getContext(), R.layout.item_menulist_lv_header, null);
				ImageView iv = (ImageView) itemView.findViewById(R.id.menulist_header_iv);
				TextView tv = (TextView) itemView.findViewById(R.id.menulist_header_tv);
				tv.setText("" + supports.get(i).getSuppor());
				Picasso.with(getContext()).load(supports.get(i).getPicUrl()).placeholder(tu).error(tu).into(iv);
				header_layout.addView(itemView);
			}
		}
		mGoodsModel.dataMatching();
		listAdapter.notifyDataSetChanged();
		item_menulist_lv.getRefreshableView().setSelection(0);
	}

	/**
	 * 构建数据
	 */
	protected void constructionData() {
		adapterData.clear();
		adapterData.addAll(goodsListRes.getData());
	}

	protected void setupIdleListener(AbsListView list) {
		item_menulist_lv.setOnRefreshListener(new OnRefreshListener<ListView>() {

			@Override
			public void onRefresh(PullToRefreshBase<ListView> refreshView) {
				if (refreshView.isHeaderShown()) {
					pageNext = 1;
					refreshData(true);
				}
			}
		});
		item_menulist_lv.setOnPreRefreshingAnimListener(new OnPreRefreshingAnimListener() {

			@Override
			public void onPreRefreshingAnim() {
				item_menulist_lv.setFooterLoadingViewHeaderText("加载更多商品");

			}
		});
		item_menulist_lv.setOnLastItemVisibleListener(new OnLastItemVisibleListener() {
			@Override
			public void onLastItemVisible() {
				if (pageNext == 0) {
					// 没有下一页了
					item_menulist_lv.onRefreshComplete();
					if (footerView == null) {
						footerView = (LinearLayout) View.inflate(mContext, R.layout.item_pull_to_refresh_footer, null);
						TextView footer = (TextView) footerView.findViewById(R.id.footer);
						// footer.setText("到底了呢-共" + adapterData.size() +
						// "件商品");
						footer.setText("到底了呢");
						item_menulist_lv.getRefreshableView().addFooterView(footerView);
					}
				} else {
					// 滑动到底部自动刷新
					refreshData(false);
				}
			}
		});
	}

	public void refreshData(boolean clean) {
		// progress.show();
		lastTask = new RefreshDataTask(lastTask.getID() + 1, clean);
		executeTask(lastTask);
	}

	private int pageNext = 1;

	/**
	 * 丢弃老数据重新刷新
	 * 
	 * @author longtao.li
	 * 
	 */
	class RefreshDataTask extends Task {

		boolean flag = false;// 是否清空原数据

		/**
		 * 是否清除旧数据
		 * 
		 * @param id
		 * @param flag
		 */
		public RefreshDataTask(int id, boolean flag) {
			super(id);
			this.flag = flag;
		}

		@Override
		public void run() {
			post(new Runnable() {
				@Override
				public void run() {
					if (footerView != null) { // 删除最底部的一条
						item_menulist_lv.getRefreshableView().removeFooterView(footerView);
						footerView = null;
					}
				}
			});

			if (pageNext == 0) {
				post(new Runnable() {
					public void run() {
						mGoodsModel.dataMatching();
						listAdapter.notifyDataSetChanged();
						item_menulist_lv.onRefreshComplete();
						showViewById(VIEW_SHOPLIST);
					}
				});
				return;
			}
			Result<List<Goods>> result = null;
			if( item == null ){
				showToastOnUI("获取商品列表出粗啦 请退出界面再进来重试");
				return;
			}
			try {
				goodsListReq.setsId(item.getsId());
				goodsListReq.setPage(pageNext);
				goodsListReq.setKey(Constant.shoppingkey);
				result = mShopsModel.v2_2queryGoodsList(goodsListReq);
			} catch (RetrofitError e) {
				showViewById(VIEW_SHOPLIST);
				switch (e.getKind()) {
				case HTTP:
					showViewById(VIEW_OUTTIME);
					break;
				case CONVERSION:
					showViewById(VIEW_LOADFAILURE);
					break;
				case NETWORK:
					showViewById(VIEW_WIFIFAILUER);
					break;
				case UNEXPECTED:
					showViewById(VIEW_LOADFAILURE);
					break;
				}
			}
			// 判断是不是用户的最后操作,最后任务的ID如果比此任务的ID大则丢弃请求结果
			if (getID() < lastTask.getID()) {
				showViewById(VIEW_SHOPLIST);
				return;
			}

			if (result != null && result.code == 1) {
				refreshUI(result, flag);
			} else {
				showViewById(VIEW_SHOPLIST);
			}

		}

		/**
		 * 刷新UI
		 * 
		 * @param result
		 * @param flag
		 */
		void refreshUI(final Result<List<Goods>> result, final boolean flag) {
			mActivity.runOnUiThread(new Runnable() {
				@Override
				public void run() {
					showViewById(VIEW_SHOPLIST);
					// dismissProgressDialog();

					List<Goods> list = result.getData();

					if (list == null || list.size() == 0) {// 没有数据返回
						adapterData.clear();
						item_menulist_lv.onRefreshComplete();
						listAdapter.notifyDataSetChanged();
					} else {

						if (!flag) {// 有数据返回
							pageNext = result.page;
							adapterData.addAll(list);
							// constructionData();//重新构建ListView的数据源
							mGoodsModel.dataMatching();
							listAdapter.notifyDataSetChanged();
							item_menulist_lv.onRefreshComplete();
						} else {// 有数据返回但需要回到顶部
							adapterData.clear();
							adapterData.addAll(list);
							mGoodsModel.dataMatching();
							// constructionData();//重新构建ListView的数据源
							pageNext = result.page;

							listAdapter.notifyDataSetChanged();
							item_menulist_lv.getRefreshableView().setSelection(0);
							item_menulist_lv.onRefreshComplete();
						}
						goodsListReq.setPage(pageNext);
					}

				}
			});

		}
	}

	/**
	 * 只接受预定
	 */
	public void onlyAcceptReservation() {
		item_menulist_shoppingcar.setBackgroundResource(R.raw.shopping_cart);
		item_menulist_shoppingcar_tv.setVisibility(View.GONE);
		if (shopDetail.getBookedTime() != null) {
			item_menulist_summoney.setText("仅接受预定" + shopDetail.getBookedTime() + "开始配送");
		} else {
			item_menulist_summoney.setText("仅接受预定");
		}
		item_menulist_submit.setVisibility(View.GONE);
		item_menulist_submit.setOnClickListener(null);
	}

	/**
	 * 店铺未营业
	 */
	public void shopNotBussiness() {
		item_menulist_shoppingcar_tv.setVisibility(View.GONE);
		item_menulist_shoppingcar.setBackgroundResource(R.raw.shopping_cart3);
		item_menulist_summoney.setText("店铺休息不接单");
		item_menulist_submit.setBackgroundColor(getResources().getColor(R.color.transparent));
		item_menulist_submit.setText("");
		item_menulist_shoppingcar.setOnClickListener(null);
		item_menulist_submit.setOnClickListener(null);
	}

	/**
	 * 店铺不在范围内
	 */
	public void shopOutsideRange() {
		item_menulist_shoppingcar_tv.setVisibility(View.GONE);
		item_menulist_shoppingcar.setBackgroundResource(R.raw.shopping_cart3);
		item_menulist_summoney.setText("店铺超出范围 不接单");
		item_menulist_submit.setBackgroundColor(getResources().getColor(R.color.transparent));
		item_menulist_submit.setText("");
		item_menulist_shoppingcar.setOnClickListener(null);
		item_menulist_submit.setOnClickListener(null);
	}

	/**
	 * 未选商品
	 */
	public void notSelected() {
		item_menulist_shoppingcar_tv.setVisibility(View.GONE);
		item_menulist_shoppingcar.setBackgroundResource(R.raw.shopping_cart);
		item_menulist_summoney.setText("什么都没选");
		item_menulist_submit.setVisibility(View.VISIBLE);
		item_menulist_submit.setBackgroundColor(getResources().getColor(R.color.transparent));
		if (shopDetail != null) {
			item_menulist_submit.setText(ParseUtils.String2Money(shopDetail.getToSendPrice()) + "元起送");
		}
		item_menulist_shoppingcar.setOnClickListener(null);
		item_menulist_submit.setOnClickListener(null);
	}

	/**
	 * 未达起送价
	 */
	public void notSendUp() {
		item_menulist_shoppingcar.setBackgroundResource(R.raw.shopping_cart2);
		item_menulist_shoppingcar_tv.setVisibility(View.VISIBLE);
		item_menulist_shoppingcar_tv.setText("" + mShoppingCarModel.getTotal());
		item_menulist_summoney.setText(
				Html.fromHtml("共  ¥<big>" + ParseUtils.formatPrice(mShoppingCarModel.getTotalCost()) + "</big>"));
		item_menulist_submit.setVisibility(View.VISIBLE);
		item_menulist_submit.setBackgroundColor(Color.parseColor("#aaaaaa"));
		item_menulist_submit.setText(Html.fromHtml("<small>还差</small>￥"
				+ ParseUtils.String2Money((shopDetail.getToSendPrice() - mShoppingCarModel.getBaseCost()))
				+ "元<small>起送</small>"));
		item_menulist_shoppingcar.setOnClickListener(this);
		item_menulist_submit.setOnClickListener(null);
	}

	/**
	 * 可以起送
	 */
	public void canSend() {
		item_menulist_shoppingcar.setBackgroundResource(R.raw.shopping_cart2);
		item_menulist_shoppingcar_tv.setVisibility(View.VISIBLE);
		item_menulist_shoppingcar_tv.setText("" + mShoppingCarModel.getTotal());
		item_menulist_summoney.setText(
				Html.fromHtml("共  ¥<big>" + ParseUtils.formatPrice(mShoppingCarModel.getTotalCost()) + "</big>"));
		item_menulist_submit.setVisibility(View.VISIBLE);
		item_menulist_submit.setBackgroundColor(Color.parseColor("#4dc027"));
		item_menulist_submit.setText("选好了");
		item_menulist_shoppingcar.setOnClickListener(this);
		item_menulist_submit.setOnClickListener(this);
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.item_menulist_shoppingcar:
			if (layout_menulist_shoppingcar.getVisibility() == View.VISIBLE) {
				layout_menulist_shoppingcar.hide();
			} else {
				layout_menulist_shoppingcar.show();
			}
			break;
			
		case R.id.item_menulist_submit:
			if (!NetWorkCenter.isNetworkConnected(getContext())) {
				showToast("网络根本没有连，赶紧检查");
				return;
			}
			mFragmentControl.closeBtn();
			SendOrderData data = new SendOrderData();
			data.setShopItem(item);
			data.setShopDetail(shopDetail);
			Info<SendOrderData> info = new Info<SendOrderData>();
			info.setData(data);
			info.setOrigin(ShopInfoActivity.class);
			Utils.openActivity(mContext, ConfirmOrderActivity_.class, info);
			break;
		}
	}

	private FragmentControl mFragmentControl;

	public void setFragmentControl(FragmentControl control) {
		this.mFragmentControl = control;
	}

	/**
	 * 更新购物车总价
	 */
	public void refreshSumMoney() {
		if (item == null)
			return;
		item_menulist_bussiness.setVisibility(View.VISIBLE);
		double sumMoney = mShoppingCarModel.getTotalCost();
		if (sumMoney == 0) {
			if (mShoppingCarModel.getGoodsList().size() == 0) {
				if (item.getState() == 1) {
					notSelected();
				} else if (item.getState() == 3) {
					onlyAcceptReservation();
				}
			} else {
				notSendUp();
			}
		} else if (!mShoppingCarModel.achieveSend()) {
			notSendUp();
		} else {
			canSend();
		}
	}

	/**
	 * 显示商品属性
	 */
	// @Override
	// public void showPropertyLayout(Goods goods) {
	// propertylayout.refreshSpecView(goods);
	// propertylayout.setVisibility(View.VISIBLE);
	//
	// }

	public void clear() {
		goodsListReq.setsType("");
		pageNext = 1;
	}

	public void setSType(String stype) {
		goodsListReq.setsType(stype);
	}

	/**
	 * 防止View的事件穿透
	 */
	@SuppressLint("ClickableViewAccessibility")
	@Override
	public boolean onTouchEvent(MotionEvent event) {
		return true;
	}

	/**
	 * 查询是否有赠品
	 */
	public void checkGiftList() {
		if( shopDetail == null ){
			showToast("查询赠品出错");
			return;
		}
		if (mActivitiesModel.isMeet()) {
			// item_menulist_submit.setOnClickListener(null);
			// boolean flag = false;
			// if(
			// result!=null&&result.getList()!=null&&result.getList().size()==1
			// ){
			// flag = true;//上一次是否剩下一个赠品,如果下次没有赠品了需要刷新UI
			// }
			
			executeTask(new Runnable() {
				
				@Override
				public void run() {
					ActivitiesReq req = new ActivitiesReq();
					req.setsId(shopDetail.getsId());
					req.setGoodsList(mShoppingCarModel.getOrderGoodsItemList());
					final CodeListResult<Gift> result = mActivitiesModel.queryActivities(req);
					runOnUI(new  Runnable() {
						public void run() {
							mShoppingCarModel.calculate();
							LogUtil.i("calculate", "checkGiftList 算完了");

							if (layout_menulist_shoppingcar.isShown()) {
								// if( (result.getList()!=null&&result.getList().size()>0) ||
								// flag){
										EventBus.getDefault().post(new EventRestShoppingCarData());

								// }
							} else {
								if (result != null && result.getList() != null && result.getList().size() > 0) {
									if (!mActivitiesModel.isAlreadySuggested(result.getList())) {
										if (mActivitiesModel.isMeet1()) {
											hintGift();
										}
									}

								}
							}
						    refreshSumMoney();
						}
					});
					
				}
			});
				
		}

	}

	private void hintGift() {
		post(new Runnable() {
			@Override
			public void run() {
				SuperUI.openBottomDialog(mContext, "您选购的商品满足优惠购活动，请在购物篮中选购！", "知道了", new OnClickListener() {
					@Override
					public void onClick(View arg0) {
						SuperUI.dismiss();
					}
				}, "去瞧瞧", new OnClickListener() {

					@Override
					public void onClick(View arg0) {
						layout_menulist_shoppingcar.show();
						SuperUI.dismiss();
					}
				});
			}
		});
	}


	public boolean isShoppingCarShow() {
		if (layout_menulist_shoppingcar.getVisibility() == View.VISIBLE) {
			return true;
		}
		return false;
	}

	public void hideShoppingCar() {
		layout_menulist_shoppingcar.hide();
	}

	/**
	 * 页面控制接口
	 * 
	 * @author longtao.li
	 * 
	 */
	public interface LayoutControl {
		/**
		 * 显示套餐明细
		 * 
		 * @param goods
		 */
		void showPropView(Goods goods);

		/**
		 * 显示已经选择的属性布局
		 * 
		 * @param goods
		 * @param adjustChosen
		 */
		void showPropView(Goods goods, ChosenGoods adjustChosen);
		
		/**
		 * 检查赠品
		 */
		public void checkGiftList();
	}

	/**
	 * 网络加载失败
	 */
	public void netFailOperate() {
		boolean isNetConnected = NetWorkCenter.isNetworkConnected(getContext());
		if (!isNetConnected) {
			showViewById(VIEW_WIFIFAILUER);
		}
	}

	/**
	 * 显示列表：,1: 显示断网：2 显示加载数据失败：3 显示加载中：4 显示请求超时:5
	 */
	public void showViewById(final int i) {
		post(new Runnable() {
			@Override
			public void run() {
				item_menulist.setVisibility(View.GONE);
				item_menulist_lv.onRefreshComplete();
				switch (i) {
				case 1:
					item_menulist.setVisibility(View.VISIBLE);
					hintView.close();
					break;
				case 2:
					hintView.netError(new OperateListener() {
						@Override
						public void operate() {
							showViewById(VIEW_LOADING);
							refreshData(true);
						}
					});
					break;
				case 3:
					hintView.loadFailure(new OperateListener() {
						@Override
						public void operate() {
							showViewById(VIEW_LOADING);
							refreshData(true);
						}
					});
					break;
				case 4:
					hintView.loadingData();
					break;
				case 5:
					hintView.timeOut(new OperateListener() {
						@Override
						public void operate() {
							showViewById(VIEW_LOADING);
							refreshData(true);
						}
					});
					break;
				}
			}
		});
	}

	@Override
	public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
		item_menulist_lv.onScroll(view, firstVisibleItem, visibleItemCount, totalItemCount);
	}

	@Override
	public void onScrollStateChanged(AbsListView view, int state) {
		item_menulist_lv.onScrollStateChanged(view, state);

	}
}
